<template>
  <div class="goods-box">
    <div class="goods-img">
      <div class="img-big">
        <img :src="onShowPic" alt="预览">
      </div>
      <div class="img-small-box">
        <div
            v-for="(img,index) in goods.images"
            :key="index"
            class="img-small">
          <img
              :src="img"
              alt="预览图${index}"
              @mouseover="showDetailPic(index)">
        </div>
      </div>
    </div>
    <div class="goods-info">
      <div class="goods-info-header">{{ goods.text }}</div>
      <div class="goods-info-price">
        <div class="text-line">
          <p class="gray-title">价格：</p>
          <p class="price-origin">￥{{ goods.price }}</p>
        </div>
        <div class="text-line">
          <p class="gray-title" style="line-height:31px">促销价：</p>
          <p class="price-online">￥{{ goods.onlinePrice }}</p>
        </div>
      </div>
      <div class="goods-info-trans">
        <div class="text-line">
          <p class="gray-title">发货地：</p>
          <p>{{ goods.location }}</p>
        </div>
        <div class="text-line">
          <p class="gray-title">收货方式：</p>
          <select v-model="chargeWay">
            <option
                v-for="(method,index) in receives"
                :value="method"
                :key="index">
              {{ method }}
            </option>
          </select>
        </div>
      </div>
      <div class="goods-info-count">
        <div class="count-btn" @click="()=>{if(this.count>1){this.count--}}">-</div>
        <input class="count-input" v-model="count">
        <div class="count-btn" @click="()=>{this.count++}">+</div>
      </div>
      <div class="goods-info-operate">
        <div class="operate-btn operate-purchase" @click="toPurchase">立即购买</div>
        <div class="operate-btn operate-list" @click="addGoodsList">加入购物车</div>
        <transition name="add-goods">
          <img
              v-if="showPic"
              class="comic-pic"
              :src="goods.poster"
              alt="" ref="showPic">
        </transition>
      </div>
      <div class="goods-info-help">
        <div
            v-for="(category,index) in helps"
            :key="index"
            @click="toHelp(category.name)"
            class="help-func">
          {{ category.text }}
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import config from "@/config/config"
export default {
  name: "goods",
  data () {
    return {
      goods: {},
      helps: [],
      receives: [],
      goodsList: [],
      goodsNames: [],
      count: 1,
      showPic: 0,
      onShowPic: "",
      chargeWay: "",
    }
  },
  mounted () {
    this.goods = config.goods[this.$route.query.id]
    this.onShowPic = this.goods.poster
    console.log(this.goods)
    this.helps = config.helps
    this.receives = config.receives
    this.chargeWay = this.receives[0]
  },
  methods: {
    toPurchase () {
      this.$router.push({
        name: "MainPage",
        params: {
          id: this.$route.query.id,
          count: this.count
        }
      })
    },
    addGoodsList () {
      this.showPic = 1
      if (this.goodsList.length === 0) {
        this.goodsList = JSON.parse(sessionStorage.getItem('goodsList'))
        if (this.goodsList === null) {
          this.goodsList = []
          this.goodsNames = []
        } else {
          this.goodsNames = this.goodsList.map(item=>item.id)
        }
      }
      if (this.goodsNames.indexOf(this.$route.query.id) === -1) {
        this.goodsList.push({
          id: this.$route.query.id,
          count: this.count
        })
        this.goodsNames.push(this.$route.query.id)
      } else {
        this.goodsList[this.goodsNames.indexOf(this.$route.query.id)].count += this.count
      }
      sessionStorage.setItem('goodsList', JSON.stringify(this.goodsList))
      console.log(this.goodsList)
    },
    toHelp (sort) {
      this.$router.push(sort)
    },
    showDetailPic (index) {
      this.onShowPic = this.goods.images[index]
    },
  },
  watch: {
    showPic () {
      if (this.showPic !== 0) {
        let self = this
        setTimeout(function () {
          self.showPic = 0
          // console.log(self.$refs.showPic.getBoundingClientRect().left)
        }, 10)
      }
    }
  },
}
</script>
<style scoped>
.add-goods-enter-active, .add-goods-leave-active{
  transition: transform .5s;
}
.add-goods-leave-to{
  transform: translate(400px,-450px)scale(.1,.1);
}
.goods-box{
  display: flex;
  padding: 0 10%;
}
.goods-img{
  width: 248px;
}
.img-big{
  height: 248px;
  margin-bottom: 20px;
}
.img-small-box{
  display: flex;
}
.img-small{
  width: 40px;
  height: 40px;
  margin: 4px;
  cursor: pointer;
}
.img-small:hover{
  border: 4px red solid;
  margin: 0;
}
.img-big img,.img-small img{
  width: 100%;
  height: 100%;
}
.goods-info{
  width: 80%;
  margin-left: 60px;
  text-align: left;
}
.goods-info p{
  margin: 8px 0;
}
.goods-info-header{
  font-weight: bold;
  padding-left: 8px;
  margin-bottom: 16px;
}
.goods-info-price{
  background-color: rgb(243, 200, 200);
  margin-bottom: 8px;
}
.text-line{
  display: flex;
}
.gray-title{
  color: gray;
  width: 100px;
  padding-left: 8px;
}
.price-origin{
  text-decoration: line-through;
}
.price-online{
  font-size: 24px;
  font-weight: bold;
}
.goods-info-trans{
  margin-bottom: 16px;
}
.goods-info-count{
  display: flex;
}
.count-btn{
  width: 32px;
  height: 32px;
  line-height: 32px;
  font-size: 24px;
  font-weight: bold;
  cursor: pointer;
  text-align: center;
}
.count-input{
  font-size: 18px;
  width: 50px;
  padding: 0 8px;
  text-align: right;
}
.goods-info-operate{
  display: flex;
  text-align: center;
  cursor: pointer;
  position: relative;
}
.operate-btn{
  width: 20%;
  height: 40px;
  line-height: 40px;
  margin: 16px 0 8px 8px;
}
.operate-purchase{
  background-color: bisque;
}
.operate-list{
  background-color: orangered;
  color: white;
}
.comic-pic{
  position: absolute;
  width: 160px;
  height: 120px;
}
.goods-info-help{
  display: flex;
}
.help-func{
  margin: 8px;
  cursor: pointer;
  color: gray;
  font-size: 14px;
}
</style>